package com.lizardmind.haofudoctor.other.rongYun

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.blankj.utilcode.util.SpanUtils
import com.linxiao.framework.util.color
import com.linxiao.framework.util.toasts
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.other.OnClickRongTipsEvent
import io.rong.imkit.model.ProviderTag
import io.rong.imkit.model.UIMessage
import io.rong.imkit.widget.provider.IContainerItemProvider
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.find

/**
 * TODO 在用户端需要在ProviderTag里面设置 hide = true ，因为不能让用户看见这条消息
 * Created by Extends on 2017/12/4 16:12
 */
@ProviderTag(messageContent = FanganTipsMessage::class,showPortrait = false,centerInHorizontal = true,showSummaryWithName = false)
class FanganTipsMessageItemProvider : IContainerItemProvider.MessageProvider<FanganTipsMessage>() {
    override fun onItemClick(p0: View, p1: Int, p2: FanganTipsMessage, p3: UIMessage) {
        EventBus.getDefault().post(OnClickRongTipsEvent(p2.id))
    }

    override fun bindView(v: View, position: Int, content: FanganTipsMessage, message: UIMessage) {
        val holder = v.tag as ViewHolder
        holder.tips?.text = if(content.custom==null || content.custom=="0"){
            SpanUtils().append("系统将在指定时间向TA发送随访复诊单，如不需要你也可以更改")
                    .append("自定义随访时间").setUnderline().setForegroundColor(v.context.color(R.color.ry_color_fangan))
                    .create()
        }else{
            SpanUtils().append("设置成功！系统将在")
                    .append(content.endTime).setForegroundColor(v.context.color(R.color.ry_color_fangan))
                    .append("向TA发送随访复诊时间")
                     .create()
        }
    }

    override fun getContentSummary(p0: FanganTipsMessage): Spannable {
        return SpannableString(if(p0.custom==null || p0.custom == "0") "[方案消息]" else "[自定义随访复诊时间]")
    }

    override fun newView(p0: Context, p1: ViewGroup): View {
        val view = LayoutInflater.from(p0).inflate(R.layout.ry_item_fangan_tips,p1,false)
        val holder = ViewHolder()
        holder.tips = view.find(R.id.rift_tips)
        view.tag = holder
        return view
    }

    class ViewHolder{
        var tips: TextView?=null
    }
}